//
//  RanHeaderView.m
//  ThusMyStyle
//
//  Created by Ran on 15/9/30.
//  Copyright (c) 2015年 gintong. All rights reserved.
//

#import "RanPagerHeaderView.h"
#import "RanPagerHeaderModel.h"
#import "UIView+Ran.h"

@interface RanPagerHeaderView()

@property(nonatomic, weak)UIScrollView *scrollView;
@property(nonatomic, weak)UIImageView *bottomLineView;

@property(nonatomic, strong)NSMutableArray *btnArrayM;
@property(nonatomic, strong)NSMutableArray *bottomLineFramesArrayM;

@property(nonatomic, strong)UIButton *selectedButton;

@end

@implementation RanPagerHeaderView

#pragma mark - System

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {

    }
    return self;
}

#pragma mark - Public

- (void)scrollButtonToVisuableAtIndex:(NSInteger)index
{
    UIButton *button = self.btnArrayM[index];
    [self.scrollView scrollRectToVisible:button.frame animated:YES];
    [self setButtonSelectedAtIndex:index];
}

- (void)scrollBottomLineToValue:(CGFloat)value
{
    int maxNum = ceil(value);
    int minNum = floor(value);
    CGFloat offsetValue = value - minNum;
    CGRect maxFrame = [self.bottomLineFramesArrayM[maxNum] CGRectValue];
    CGRect minFrame = [self.bottomLineFramesArrayM[minNum] CGRectValue];
    CGFloat x = minFrame.origin.x + offsetValue * (maxFrame.origin.x - minFrame.origin.x);
    CGFloat y = minFrame.origin.y;
    CGFloat width = minFrame.size.width + offsetValue * (maxFrame.size.width - minFrame.size.width);
    CGFloat height = minFrame.size.height;
    CGRect currentFrame = CGRectMake(x, y, width, height);
    self.bottomLineView.frame = currentFrame;
    
    UIButton *minButton = self.btnArrayM[minNum];
    UIButton *maxButton = self.btnArrayM[maxNum];
    
    CGFloat normalRed;
    CGFloat normalGreen;
    CGFloat normalBlue;
    CGFloat normalAlpla;
    CGFloat selectedRed;
    CGFloat selectedGreen;
    CGFloat selectedBlue;
    CGFloat selectedAlpha;
    [self.model.normalStateColor getRed:&normalRed green:&normalGreen blue:&normalBlue alpha:&normalAlpla];
    [self.model.selectedStateColor getRed:&selectedRed green:&selectedGreen blue:&selectedBlue alpha:&selectedAlpha];
    
    UIColor *minColor = [UIColor colorWithRed:normalRed + (selectedRed - normalRed) * offsetValue green:normalGreen + (selectedGreen - normalGreen) * offsetValue blue:normalBlue + (selectedBlue - normalBlue) * offsetValue alpha:normalAlpla + (selectedAlpha - normalAlpla) * offsetValue];
    UIColor *maxColor = [UIColor colorWithRed:normalRed + (selectedRed - normalRed) * (1- offsetValue) green:normalGreen + (selectedGreen - normalGreen) * (1- offsetValue) blue:normalBlue + (selectedBlue - normalBlue) * (1- offsetValue) alpha:normalAlpla + (selectedAlpha - normalAlpla) * (1- offsetValue)];
    
    [maxButton setTitleColor:minColor forState:UIControlStateNormal];
    [maxButton setTitleColor:minColor forState:UIControlStateSelected];
    [minButton setTitleColor:maxColor forState:UIControlStateNormal];
    [minButton setTitleColor:maxColor forState:UIControlStateSelected];
    
    CGFloat minFontSize = self.model.normalStateFontSize + (self.model.selectedStateFontSize - self.model.normalStateFontSize) * offsetValue;
    CGFloat maxFontSize = self.model.normalStateFontSize + (self.model.selectedStateFontSize - self.model.normalStateFontSize) * (1 - offsetValue);
    maxButton.titleLabel.font = [UIFont systemFontOfSize:minFontSize];
    minButton.titleLabel.font = [UIFont systemFontOfSize:maxFontSize];
    
    NSLog(@"value: %f, min: %d, max: %dx ", value, minNum, maxNum);
    NSLog(@"%@", NSStringFromCGRect(currentFrame));
}

#pragma mark - Private

- (void)setButtonSelectedAtIndex: (NSInteger)index
{
    self.selectedButton.titleLabel.font = [UIFont systemFontOfSize:self.model.normalStateFontSize];
    self.selectedButton.selected = NO;
    
    UIButton *button = self.btnArrayM[index];
    button.titleLabel.font = [UIFont systemFontOfSize:self.model.selectedStateFontSize];
    button.selected = YES;
    self.selectedButton = button;

    
    if(self.model.showBottomLine){
        [UIView animateWithDuration:0.1 animations:^{
//            self.bottomLineView.frame = [self.bottomLineFramesArrayM[index] CGRectValue];
        }];
    }
}

#pragma mark - Delegate

#pragma mark - Event

- (void)btnClicked: (UIButton *)btn
{
    if (!btn.selected) {
        self.btnEventBlock(btn.tag);
        [self setButtonSelectedAtIndex:btn.tag];
    }
}

#pragma mark - Getter & Setter

- (void)setModel:(RanPagerHeaderModel *)model
{
    _model = model;
    
    //先不考虑重用的情况
    CGFloat btnX = 0;
    CGFloat btnY = 0;
    CGFloat btnW = 0;
    CGFloat btnH = self.model.showSeperator? self.scrollView.ranHeight - self.model.bottomLineHeight: self.scrollView.ranHeight;
    
    CGFloat seperatorY = self.model.seperatorInsets.top;
    CGFloat seperatorW = 4 - self.model.seperatorInsets.left - self.model.seperatorInsets.right;
    CGFloat seperatorH = btnH - self.model.seperatorInsets.bottom - seperatorY;
    if(seperatorH < 0) seperatorH = 0;
    if(seperatorW < 0) seperatorW = 0;

    for (NSInteger i = 0; i < self.model.titleArray.count; i++) {
        
        NSString *title = self.model.titleArray[i];
        CGFloat titleW = [title boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:self.model.selectedStateFontSize]} context:NULL].size.width;
        
        if (self.model.headerButtonStyle == RanPagerHeaderButtonStyleConstantWidth) {
            btnW = self.model.headerButtonWidth;
        }else if(self.model.headerButtonStyle == RanPagerHeaderButtonStyleTextWidthAndPadding){
            btnW = titleW + 2 * self.model.headerButtonPadding;
        }
        
        UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(btnX, btnY, btnW, btnH)];
        btn.backgroundColor = [UIColor clearColor];
        btn.selected = i? NO: YES;
        btn.tag = i;
        [btn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
        btnX += btnW;
        
        [btn setTitle:title forState:UIControlStateNormal];
        [btn setTitleColor:self.model.normalStateColor forState:UIControlStateNormal];
        [btn setTitle:title forState:UIControlStateSelected];
        [btn setTitleColor:self.model.selectedStateColor forState:UIControlStateSelected];
        btn.titleLabel.font = [UIFont systemFontOfSize:self.model.normalStateFontSize];
        
        [self.scrollView addSubview:btn];
        [self.btnArrayM addObject:btn];
        
        if (self.model.showBottomLine) {
            
            CGFloat bottomLineHeight = self.model.bottomLineHeight;
            CGFloat bottomLineY = self.ranHeight - bottomLineHeight;
            CGFloat bottomLineWidth = self.model.headerBottomLineStyle == RanPagerHeaderBottomLineStyleWithinText? titleW: btnW;
            CGFloat bottomLineX = btnW / 2 - bottomLineWidth / 2 + btn.ranX;
            CGRect bottomLineFrame = CGRectMake(bottomLineX, bottomLineY, bottomLineWidth, bottomLineHeight);
            [self.bottomLineFramesArrayM addObject:[NSValue valueWithCGRect:bottomLineFrame]];
            
            if (self.model.bottomLineMoveInRealTime) {
                
            }else{
            
            }
        }
        
        if (i == self.model.titleArray.count - 1) {
            self.scrollView.contentSize = CGSizeMake(btnX, self.scrollView.ranHeight);
        }
        
        if (i != self.model.titleArray.count - 1 && self.model.showSeperator) {
            UIImageView *seperator = [[UIImageView alloc] init];
            seperator.backgroundColor = self.model.seperatorColor;
            seperator.ranSize = CGSizeMake(seperatorW, seperatorH);
            seperator.ranY = seperatorY;
            seperator.ranCenterX = btnX;
            [self.scrollView addSubview:seperator];
        }
        
    }
    
    if (self.model.showBottomLine) {
        self.bottomLineView.frame = [self.bottomLineFramesArrayM[0] CGRectValue];
        self.bottomLineView.backgroundColor = self.model.bottomLineColor;
    }
    
    [self setButtonSelectedAtIndex:0];
    
}

- (UIScrollView *)scrollView
{
    if (!_scrollView) {
        UIScrollView *scrollView = [[UIScrollView alloc] init];
        scrollView.showsHorizontalScrollIndicator = NO;
        scrollView.showsVerticalScrollIndicator = NO;
        scrollView.backgroundColor = [UIColor clearColor];
        scrollView.frame = self.bounds;
        [self addSubview:scrollView];
        self.scrollView = scrollView;
    }
    return _scrollView;
}

- (UIImageView *)bottomLineView
{
    if (!_bottomLineView) {
        UIImageView *bottomLineView = [[UIImageView alloc] init];
        bottomLineView.backgroundColor = [UIColor orangeColor];
        [self.scrollView addSubview:bottomLineView];
        self.bottomLineView = bottomLineView;
    }
    return _bottomLineView;
}

- (NSMutableArray *)btnArrayM
{
    if (!_btnArrayM) {
        self.btnArrayM = [NSMutableArray array];
    }
    return _btnArrayM;
}

- (NSMutableArray *)bottomLineFramesArrayM
{
    if (!_bottomLineFramesArrayM) {
        self.bottomLineFramesArrayM = [NSMutableArray array];
    }
    return _bottomLineFramesArrayM;
}

@end
